import path from 'path';
import { defineConfig } from 'vite';
import uni from '@dcloudio/vite-plugin-uni';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';

const pathSrc = path.resolve(__dirname, 'src');
const pathTypes = path.resolve(pathSrc, 'types');

// https://vitejs.dev/config/
export default defineConfig({
  server: {
    port: 5173,
    open: true,
    cors: true,
    proxy: {
      '/diet': {
        // target: 'http://192.168.1.27:7001',
        target: 'http://8.134.250.251:2284',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/diet/, ''),
      },
    },
  },
  resolve: {
    alias: {
      '@': pathSrc,
    },
    extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json'],
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `
        @import "@/styles/global.scss";
        @import "@/styles/common.scss";
      `,
      },
    },
  },
  plugins: [
    Components({
      dirs: ['src/components'],
      dts: path.resolve(pathTypes, 'components.d.ts'),
    }),
    uni(),
    AutoImport({
      imports: [
        'vue',
        {
          '@dcloudio/uni-app': [
            'onLoad',
            'onShow',
            'onHide',
            'onUnload',
            'onReady',
            'onLaunch',
            'onPullDownRefresh',
          ],
          '@/apis': [['default', '$api']],
          '@/stores': [['default', '$store']],
        },
      ],
      dts: path.resolve(pathTypes, 'auto-imports.d.ts'),
      eslintrc: {
        enabled: true,
        filepath: './.eslintrc-auto-import.json',
        globalsPropValue: 'readonly',
      },
    }),
  ],
});
